﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;

//using Microsoft.Office.Interop;

namespace Ylh.Office
{
    public class WordXLS
    {
        #region Data Members

        public string Message = string.Empty;           
        private Word.Application OfficeWord;       
        private Excel.Application OfficeExcel;     
        object Unknown = Type.Missing;		            
        public enum StatusType { SUCCESS, FAILED };     
        public StatusType Status;                      

        #endregion

        /// <summary>
        /// World文档转化成HTML
        /// </summary>
        /// <param name="Source">World文档物理路径</param>
        /// <param name="Target">转化后的HTML文件存储路径</param>
        public void WordToHTML(object Source, object Target)
        {
            if (OfficeWord == null)                       
                OfficeWord = new Word.ApplicationClass();

            try
            {
                
                OfficeWord.Visible = false;
                OfficeWord.Application.Visible = false;
                OfficeWord.WindowState = Word.WdWindowState.wdWindowStateMinimize;


                OfficeWord.Documents.Open(ref Source, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown);

                object format = Word.WdSaveFormat.wdFormatHTML;

                OfficeWord.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                       ref Unknown, ref Unknown);

                Status = StatusType.SUCCESS;
                Message = Status.ToString();
            }
            catch (Exception e)
            {
                Message = "Error :" + e.Message.ToString().Trim();
            }
            finally
            {
                if (OfficeWord != null)
                {
                    OfficeWord.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                    OfficeWord.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
            }
        }

        /// <summary>
        /// Excel转化成HTML文件
        /// </summary>
        /// <param name="Source">Excel文件的物理路径</param>
        /// <param name="Target">转化成HTML存储路径</param>
        public void ExcelToHTML(string Source, string Target)
        {
            if (OfficeExcel == null) OfficeExcel = new Excel.ApplicationClass();

            try
            {
                OfficeExcel.Visible = false;
                OfficeExcel.Application.Visible = false;
                OfficeExcel.WindowState = Excel.XlWindowState.xlMinimized;

                OfficeExcel.Workbooks.Open(Source, Unknown,
                     Unknown, Unknown, Unknown,
                     Unknown, Unknown, Unknown,
                     Unknown, Unknown, Unknown,
                     Unknown, Unknown, Unknown, Unknown);

                object format = Excel.XlFileFormat.xlHtml;

                OfficeExcel.Workbooks[1].SaveAs(Target, format,
                        Unknown, Unknown, Unknown,
                        Unknown, Excel.XlSaveAsAccessMode.xlExclusive, Unknown,
                        Unknown, Unknown, Unknown,
                        Unknown);

                Status = StatusType.SUCCESS;
                Message = Status.ToString();

            }
            catch (Exception e)
            {
                Message = "Error :" + e.Message.ToString().Trim();
            }
            finally
            {
                if (OfficeExcel != null)
                {
                    OfficeExcel.Workbooks.Close();
                    OfficeExcel.Quit();
                }
            }
        }



        /// <summary>
        ///// PPT文件转化成HTML
        ///// </summary>
        ///// <param name="path">PPt的物理路径</param>
        ///// <param name="savePath">转化成HTML文件存储路径</param>
        ///// <param name="wordFileName">PPt文件名（不含后缀）</param>
        //public void  PPTToHtml(string path, string savePath, string wordFileName)
        //{
        //    Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
        //    string strSourceFile = path;


        //    string[] jss = savePath.Split('.');
        //    string ext = jss[jss.Length - 1].ToString();

        //    string strDestinationFile = savePath.Remove (savePath.LastIndexOf(ext))  + "html";
        //    Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
        //    prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
        //    Status = StatusType.SUCCESS;
        //    prsPres.Close();
        //    ppApp.Quit();

        //}




    }
}
